package com.xiaoshu.controller;

import com.xiaoshu.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

/**
 * 功能说明：
 * com.xiaoshu.controller
 * 说明：
 * (1)通过 http://127.0.0.1:8080/v1/score-server/page
 * (2)通过 http://127.0.0.1:8080/v1/score-server/websocket.html (访问测试页面)
 * <p>
 * Original @Author: deane.jia-贾亮亮,2021/6/16 13:57
 * <p>
 * Copyright (C)2012-2021 小树盛凯科技 All rights reserved.
 */
@Controller
@Slf4j
public class WebSocketController {

    @GetMapping(value = "/index")
    public ResponseEntity<String> index(){
        return ResponseEntity.ok("请求成功");
    }

    @GetMapping(value = "/page")
    public String page(){
        log.info("[ModeAndView] return modeAndView");
        return "websocket";
    }

    @RequestMapping(value = "/push/{toUserId}")
    @ResponseBody
    public ResponseEntity<String> pushToWeb(String message, @PathVariable String toUserId) throws IOException {
        WebSocketServer.sendInfo(message,toUserId);
        return ResponseEntity.ok("MSG SEND SUCCESS");
    }

}
